#pragma once

#include <iostream>
#include <mutex>
#include <thread>
#include <condition_variable>
#include <functional>

#include "serial/serial.h"

#include "serial_protocol.h"



class SerialImpl
{
public:
    SerialImpl(const std::string path, int rate);
    SerialImpl(const SerialImpl&) = delete;
    SerialImpl& operator=(const SerialImpl&) = delete;
    ~SerialImpl();

    bool openSerial();
    bool reopenSerial();
    bool closeSerial();
    int sendData(uint8_t* data, size_t len);

    void start();
    void stop();

    void readData();

    //数据协议解析回调
    inline void setRxCallBack(PROCESS_CALLBACK call_back, void* param) {
        rx_call_back_ = call_back;
        rx_call_back_param_ = param;
    }

private:
    std::string serial_name_;
    int serial_rate_;
    std::unique_ptr<serial::Serial> serial_;
    bool is_open_;
    bool process_flag_;
    std::shared_ptr<std::thread> process_thr_;

    void* rx_call_back_param_ = nullptr;
    PROCESS_CALLBACK rx_call_back_;


};